package com.zeng.controller;


import com.zeng.common.Constants;
import com.zeng.dao.LeagueTableQueryDao;
import com.zeng.entity.Goods;
import com.zeng.entity.Result;
import com.zeng.entity.ResultPage;

import com.zeng.entity.Ordersinfo;
import com.zeng.service.GoodsService;
import com.zeng.service.OrdersinfoService;
import com.zeng.utils.TimeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * (Ordersinfo)表控制层
 *
 * @author zengfanchao
 * @since 2021-03-30 14:51:47
 */
@RestController
@RequestMapping("ordersinfo")
public class OrdersinfoController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersinfoService ordersinfoService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private LeagueTableQueryDao leagueTableQueryDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Ordersinfo selectOne(Integer id) {
        return this.ordersinfoService.queryById(id);
    }


    //取消订单
    @GetMapping("closeOrder")
    public Result closeOrder(Integer id) {
        Ordersinfo ordersinfo = this.ordersinfoService.queryById(id);
        ordersinfo.setGoodssum(Constants.ORDER_STATUS_CLOSE);
        this.ordersinfoService.update(ordersinfo);
        /**
         * 重新显示商品
         */
        Goods goods = this.goodsService.queryById(ordersinfo.getGoodsid());
        goods.setIsreview(1);
        this.goodsService.update(goods);
        return Result.success("取消订单");
    }

    //确认收货
    @GetMapping("getGoods")
    public Result getGoods(Integer id) {
        Ordersinfo ordersinfo = this.ordersinfoService.queryById(id);
        ordersinfo.setGoodssum(Constants.ORDER_STATUS_QUEREN1);
        this.ordersinfoService.update(ordersinfo);
        return Result.success("买家确认收货");
    }

    //确认收货
    @GetMapping("seccessorder")
    public Result seccessorder(Integer id) {
        Ordersinfo ordersinfo = this.ordersinfoService.queryById(id);
        ordersinfo.setGoodssum(Constants.ORDER_STATUS_SUCCESS);
        this.ordersinfoService.update(ordersinfo);
        /**
         * 重新显示商品
         */
        Goods goods = this.goodsService.queryById(ordersinfo.getGoodsid());
        goods.setDeleteflag(1);
        this.goodsService.update(goods);
        return Result.success("交易完成");
    }

    @GetMapping("getOrdersByUid")
    public List<HashMap<String, Object>> getOrdersByUid(Integer id) {
        return this.leagueTableQueryDao.selectOrdersByUId(id);
    }

    @GetMapping("getOrdersByid")
    public Result getOrdersByid(Integer id) {
        HashMap<String, Object> map = this.leagueTableQueryDao.selectOrdersById(id);
        return Result.success(map);
    }

    /**商家查询本人店铺的订单
     * @param id
     * @return
     */


    @GetMapping("selectSellerOrdersByUId")
    public List<HashMap<String, Object>> selectSellerOrdersByUId(Integer id) {
        return this.leagueTableQueryDao.selectSellerOrdersByUId(id);
    }

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("selectAll")
    public List<Ordersinfo> selectAll() {
        return this.ordersinfoService.selectAll();
    }

    /**
     * 根据页号查询
     *
     * @param offset 查询初始位置
     * @param limit  查询数量
     * @return
     */
    @GetMapping("selectAllByLimit")
    public ResultPage selectAllByLimit(int offset, int limit) {
        int pageTotal = this.ordersinfoService.queryCount();
        List<Ordersinfo> list = this.ordersinfoService.queryAllByLimit(offset, limit);
        return ResultPage.success(pageTotal, "查询成功", list);
    }

    @PostMapping("selectKeyByLimit")
    public ResultPage selectKeyByLimit(@RequestBody HashMap<String, String> map) {
        if (map.get("key").equals("")) {
            int pageTotal = this.ordersinfoService.queryCount();
            List<Ordersinfo> list = this.ordersinfoService.queryAllByLimit(Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            return ResultPage.success(pageTotal, "查询成功", list);
        } else {
            int pageTotal = this.ordersinfoService.queryCountSelect(map.get("key"));
            List<Ordersinfo> list = this.ordersinfoService.queryKeyByLimit(map.get("key"), Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            return ResultPage.success(pageTotal, "查询成功", list);
        }
    }

    /**
     * 根据主键删除
     *
     * @return
     */
    @GetMapping("deleteById")
    public Result deleteById(Integer id) {
        this.ordersinfoService.deleteById(id);
        return Result.success("删除成功");
    }

    @GetMapping("deleteByFlag")
    public Result deleteByFlag(Integer id) {
        Ordersinfo ordersinfo = ordersinfoService.queryById(id);
        ordersinfo.setDeleteflag(1);
        ordersinfoService.update(ordersinfo);
        return Result.success("删除成功");
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody HashMap<String, String> map
    ) {
        Ordersinfo ordersinfo = this.ordersinfoService.queryById(Integer.valueOf(map.get("orderid")));
        ordersinfo.setCreatetime(TimeUtils.NowTimeN());
        ordersinfo.setUid(Integer.valueOf(map.get("uid")));
        ordersinfo.setAllprice(Double.valueOf(map.get("allprice")));
        ordersinfo.setGoodsid(Integer.valueOf(map.get("goodsid")));
        ordersinfo.setGoodssum(Integer.valueOf(map.get("goodssum")));
        ordersinfo.setDeleteflag(Integer.valueOf(map.get("deleteflag")));
        ordersinfoService.update(ordersinfo);
        return Result.success("修改成功");
    }

    @RequestMapping("add")
    public Result add(@RequestBody HashMap<String, String> map
    ) {
        Ordersinfo ordersinfo = new Ordersinfo();
        ordersinfo.setCreatetime(TimeUtils.NowTimeN());
        ordersinfo.setUid(Integer.valueOf(map.get("uid")));
        ordersinfo.setAllprice(Double.valueOf(map.get("allprice")));
        ordersinfo.setGoodsid(Integer.valueOf(map.get("goodsid")));
        ordersinfo.setGoodssum(Integer.valueOf(map.get("goodssum")));
        ordersinfo.setDeleteflag(Integer.valueOf(map.get("deleteflag")));
        ordersinfoService.insert(ordersinfo);
        return Result.success("添加成功");
    }

    @RequestMapping("addOrders")
    public Result addOrders(@RequestBody HashMap<String, String> map
    ) {
        Ordersinfo ordersinfo = new Ordersinfo();
        ordersinfo.setCreatetime(TimeUtils.NowTimeN());
        ordersinfo.setUid(Integer.valueOf(map.get("uid")));
        ordersinfo.setAllprice(this.goodsService.queryById(Integer.valueOf(map.get("goodsid"))).getPrice());
        ordersinfo.setGoodsid(Integer.valueOf(map.get("goodsid")));
        ordersinfo.setGoodssum(1);
        ordersinfo.setDeleteflag(0);
        ordersinfoService.insert(ordersinfo);
        /**
         * 隐藏商品
         */
        Goods goods = this.goodsService.queryById(Integer.valueOf(map.get("goodsid")));
        goods.setIsreview(2);
        this.goodsService.update(goods);
        return Result.success("订单创建成功");
    }
}
